<{include file="public/header" /}>
<body>
  
<!-- 顶部搜索 -->
<div class="layui-form">
  <div class="layui-form-item" style="margin-top: 10px;">
    <!-- <label class="layui-form-label">姓名</label>
    <div class="layui-input-inline" >
      <input type="text" name="uname" autocomplete="off" id="uname" placeholder="教师姓名" class="layui-input">
    </div> -->
    <label class="layui-form-label" style="width: 30px;">学科</label>
    <div class="layui-input-inline">
      <select name="subject" lay-filter="subject" id="subject">
        <option value=""></option>
        <option value="1">语文</option>
        <option value="2">数学</option>
        <option value="3">英语</option>
        <option value="4">物理</option>
        <option value="5">化学</option>
        <option value="6">生物</option>
        <option value="7">政治</option>
        <option value="8">历史</option>
        <option value="9">地理</option>
      </select>
    </div>
    <button class="layui-btn" data-type="reload" id="search"><i class="layui-icon layui-icon-search"></i>搜索</button>
    <button class="layui-btn" data-type="reload" id="refresh"><i class="layui-icon layui-icon-refresh"></i>刷新</button>
  </div>
</div>
<!-- 顶部搜索end -->
<table class="layui-hide" id="elist" lay-filter="elist" ></table>
 
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="addData"><i class="layui-icon layui-icon-add-circle"></i>添加考试</button>
    <!-- <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="getCheckData"><i class="layui-icon layui-icon-delete"></i>批量删除</button> -->
    <!-- <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
    <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button> -->
  </div>
</script>
 
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="viewpaper">预览</a>
  <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="allocate">阅卷分配</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="statusTpl">
  <input type="checkbox"  name = "examstatus"   value = {{d.examstatus}} lay-skin="switch" lay-text="已发布|已关闭"  mid={{d.eid}}  lay-filter="examstatus" {{ d.examstatus == '2' ? 'checked' : '' }}>
</script>      
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 --> 
 <div style="width: 100%;height: 180px;"></div>
<script>
layui.use(['form','laypage','table','element','laydate','jquery'], function(){
  var table = layui.table,laypage=layui.laypage,element=layui.element,$ = layui.$,form=layui.form;
    $('#refresh').on('click',function(){
    	window.location.reload()
    });
  //数据表格配置
  table.render({
    elem: '#elist'
    ,url:'<{:url('edata')}>'
    ,toolbar: '#toolbarDemo'
    ,defaultToolbar: ['filter']
    ,title: '考试表_siku在线考试系统'
    ,cols: [[
      //{type: 'checkbox', fixed: 'left'}
      {field:'eid', title:'ID', sort: true}
      ,{field:'title', title:'考试',width:'46%',event:'exameidt', style:'cursor: pointer;',templet:function(d){return '<a title="点击可查看编辑">'+ d.title +'</a>'}}
      ,{field:'countan',title:'答卷',event:'showstuanswers',style:'cursor: pointer;',templet:function(d){return '<span class="layui-table-link">'+ d.getstuanswers_count +' 份</span>'
      }}
      ,{field:'subject', title:'科目',templet:function(d){return get_question_subject(d.subject)}}
      ,{field:'examstatus', title:'状态', sort: true,templet:'#statusTpl'}
      ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:200}
    ]]
    ,page: true
    ,id: 'hereReload'
  });
  
  //开关事件(status是lay-filter中的status，注意区别)
   form.on('switch(examstatus)', function(obj){
              // 通过属性获取绑定的id值
      var id = $(this).attr('mid');
              // 判断开关的状态
      var status=obj.elem.checked?"2":"1";
              // 构造请求参数对象
      var data=new Object();
      data.examstatus=status;
      data.eid=id;
      $.post("<{:url('change_examstatus')}>",{data:data},function(data,status){
                //layer.alert(data);
                //alert(JSON.stringify(data));
                //console(data);
                if(data===1){
                  //layer.close(index);
                  layer.msg('切换成功！'); 
                }else{
                  
                  layer.alert('切换失败！',{icon:5});
                }
             });
    });
   
  //头工具栏事件
  table.on('toolbar(elist)', function(obj){
    //console.log(obj);
    var checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
      case 'getCheckData':
        var data = checkStatus.data;
        //layer.alert(JSON.stringify(data));
        //console(data.field);
        var id_str='';
		if (data.length > 0) {
			for (var i = 0; i < data.length; i++) {
			id_str += data[i].tid + ',';
			}
			id_str = id_str.substr(0, id_str.length-1);//获取选中id
			
			//alert(id_str);
			//异步批量删除选中的数据
			layer.confirm('确定删除所选数据?', {btn: ['确定','取消'],icon: 3, title:'提示'}
	        ,function(index){
	          //do something
	            $.post("<{:url('delcheck')}>",{id:id_str},function(data,status){
	              //alert(data);
	              //alert(JSON.stringify(data));
	              //console(data);
	              if(data===1){
	                //layer.close(index);
	                layer.msg('删除成功！',{time:1000},function(){
	                  window.location.reload()
	                }); 
	              }else{
	              	
	                layer.alert('删除数据错误，请刷新重试！',{icon:5},function(index){layer.close(index);});
	              }
	           })
	          
	        });
	    }else{
	    	layer.msg('没有选中数据！')
	    }
       //  ,function(){
       //    //点击取消的返回值
       //    //layer.msg('quxiao')
       //    window.location.reload()
       // });
		//异步批量删除选中的数据end
      break;
      case 'addData':
      	layer.open({
      	  title:'添加考试'
		  ,type: 2 
		  //,offset: '10px'
		  ,area: ['900px', '450px']
		  ,content: '<{:url('add')}> '//这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
		}); 
      break;
      // case 'getCheckLength':
      //   var data = checkStatus.data;
      //   layer.msg('选中了：'+ data.length + ' 个');
      // break;
      // case 'isAll':
      //   layer.msg(checkStatus.isAll ? '全选': '未全选');
      // break;
    };
  });
  
  //监听行工具事件
  table.on('tool(elist)', function(obj){
    var data = obj.data;
    //console.log(obj);
    //alert(data.tid);
    if(obj.event === 'del'){
      layer.confirm('确定删除【'+data.title+'】吗？', function(index){
         $.post("<{:url('del')}>",{id:data.eid},function(data,status){
	          //alert(data);
	          //alert(JSON.stringify(data));
	          //console(data);
	          if(data===1){
	            layer.close(index);
	            obj.del();
	            layer.msg('删除成功！',{time:1000})
	          }else{
	          	layer.close(index);
	            layer.alert(data,{icon:5});
	          }
	     })
      });
    } else if (obj.event === 'exameidt') {
       var data=obj.data;
        // layer.alert(JSON.stringify(data), {
        //   title: '当前行数据：'
        // });
        //console.log(1);
          var str='<{:url("edit",["id"=>"tid"])}>';
          var url=str.replace("tid",data.eid);
          layer.open({
              title:'考试修改'
              ,type: 2 
              //,offset: '10px'
              ,area: ['900px', '450px']
              ,content: url //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
        });
    }else if(obj.event === 'viewpaper'){
      var data=obj.data;
      //layer.alert(data.eid);
      var str='<{:url("view",["id"=>"tid"])}>';
      var url=str.replace("tid",data.eid);
      //alert(url);
      var index=layer.open({
      	  title:data.title
		  ,type: 2 
      ,maxmin: true
		  ,area: ['900px', '450px']
		  ,content: url
      });layer.full(index);
    }else if (obj.event ==='allocate') {
      //window.location.href="<{:url('question/index')}>";
      var data=obj.data;
      //layer.alert(JSON.stringify(data));
      var nowtime= new Date(),overtime=(data.begintime+data.timelong*60)*1000;
    //alert(typeof data.qids);
      if (nowtime.getTime()< overtime) {
        layer.alert('考试尚未结束！');
      } else {
        $.post("<{:url('check_mark_human')}>",{qids:data.qids},function(data1,status){
            //alert(data);
            //alert(JSON.stringify(data));
            //console(data);
            if(data1===0){
              layer.alert('该考试无需人工阅卷！');
            }else{
              var str='<{:url("allocate",["id"=>"tid"])}>';
              var url=str.replace("tid",data.eid);
              //alert(url);
              var index=layer.open({
                  title:'【阅卷分配】'+data.title
              ,type: 2 
              ,maxmin: true
              ,area: ['500px', '450px']
              ,content: url
              });
            }
       })
      }
    }else if(obj.event === 'showstuanswers'){
      var data=obj.data;
      //layer.alert(JSON.stringify(data));
      var str='<{:url("showstuanswers",["id"=>"tid"])}>';
      var url=str.replace("tid",data.eid);
      //alert(url);
        if (data.getstuanswers_count==0) {
          layer.alert('该考试尚无数据！');
        } else {
          var index1=layer.open({
          title:data.title
          ,type: 2 
          ,maxmin: true
          ,area: ['900px', '450px']
          ,content: url
          });layer.full(index1);
        }
      
    }

  });

  



  //表格重载
	  var active = {
	    reload: function(){
	      var subject = $('#subject');//,grade = $('#grade');
	      
	      //执行重载
	      table.reload('hereReload', {
	        page: {
	          curr: 1 //重新从第 1 页开始
	        }
	        ,where: {
	          //key: {
	            subject: subject.val()//,grade:grade.val()
	          //}
	        }
	      });
	    }
	  };
	  
	  $('#search').on('click', function(){
	    var type = $(this).data('type');
	    active[type] ? active[type].call(this) : '';
	  });
})
</script>
<div style="height: 150px;background-color: white;"></div>
</body>
</html>
